package App.Job;

import App.Model.Task.CrmTask;
import App.Model.Task.CrmTaskMember;
import App.Model.Task.CrmTaskProjectRelation;
import App.Model.Task.TaskMember;
import App.Repository.FriendRepository;
import App.Repository.SMSRepository;
import App.Repository.TaskRepository;
import App.Repository.XinGePushRepository;
import App.Service.JPushService;
import App.Service.SMSDanMiService;
import App.Service.XingeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by xw on 2019/5/15.
 */

@Component
public class TaskReminderJobNormal {
    @Autowired
    private TaskRepository _taskRepository;

    @Autowired
    private FriendRepository _friendRepository;

    @Autowired
    private XingeService _xingeService;

    @Autowired
    private JPushService _jPushService;

    @Autowired
    private SMSRepository _repository;

    @Autowired
    private SMSDanMiService smsDanMiService;

    public void execute() {


        System.out.println("the ontime hour job ");

        try {
            String _title = "";
            String _finalString = "";
            Integer _userId = 1;
            SimpleDateFormat _dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            List<CrmTask> _tasks = _taskRepository.getTasksOnTime();
//            List<CrmTask> _tasks = _taskRepository.getTasksOnTime(_dataFormat.format(new Date()));

            for (CrmTask t : _tasks) {
                String _taskid = t.getRecid();
                String _taskContent = t.getContent();
                String taskBegintime = _dataFormat.format(t.getBegintime());

                SimpleDateFormat _dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String _beginTimeStr = _dateFormat.format(t.getBegintime());

                _title = _taskContent;
                _finalString = "任务：开始时间:" + taskBegintime + ",请尽快完成任务！";

                List<CrmTaskProjectRelation> _projectRelations = _taskRepository.getTaskProjectRelation(_taskid);
                if (_projectRelations != null && _projectRelations.size() > 0) {
                    _finalString = String.format("任务:%s,开始时间:%s", _taskContent, taskBegintime);
                } else {
                    _finalString = String.format("本任务的开始时间:%s", taskBegintime);
                }

                XinGePushRepository _pushRepository = new XinGePushRepository();
                List<CrmTaskMember> _members = _pushRepository.getTaskMember(_taskid);
                List<TaskMember> _tmpTaskMembers = new ArrayList<>();
                for (CrmTaskMember m : _members) {
                    TaskMember _tm = new TaskMember();
                    _tm.setUserid(m.getUserid());
                    _tm.setUsername(m.getUsername());
                    _tm.setIshost(m.ishost());
                    _tm.setIsmanager(m.ismanager());
                    _tmpTaskMembers.add(_tm);
                }

                //发送消息到group
                _taskRepository.sendIMGroupMessageForTask(_taskid, _finalString, _tmpTaskMembers, _userId, 0);

                //发送短信
                for (TaskMember m : _tmpTaskMembers) {
                    String _smsMessage = _taskRepository.getSmsMessage(m, _taskContent, taskBegintime);
                    _repository.sendMessage(m.getUsername(), _smsMessage);
                }
               /* 2022.6.2取消到期发送,修改为提醒发送 //  2022/4/29 旦米发送短信
                List<String> phones = _tmpTaskMembers.stream().map(TaskMember::getUsername).collect(Collectors.toList());
                smsDanMiService.expireTask(phones, _beginTimeStr);*/
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
